package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
   @Autowired
    private SetmealMapper setmealMapper;
   @Autowired
   private SetmealDishService setmealDishService;
    @Override
    public Page<SetmealDto> getByIdWhitDish(int page, int pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
        setmealMapper.getByIdWhitDish(pageInfo,name);
        return pageInfo;
    }
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param dto
     */
    @Override
    public void saveWhitDish(SetmealDto dto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(dto);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将dto中得id 存到遍历出来得数据中
            setmealDish.setSetmealId(dto.getId());
        }
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除   同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    public void removeWhitDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus,1);
        lqw.in(Setmeal::getId,ids);
        int count = this.count(lqw);
        //如果不能删除，抛出一个业务异常
        if (count>0){
            throw new CustomException("商品正在售卖中，不能进行删除");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(setmeallqw);
    }

    @Override
    public SetmealDto updateWhitDish(Long id ) {
        //获取id
        Setmeal setmeal = this.getById(id);
        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,dto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> list = setmealDishService.list(lqw);
            dto.setSetmealDishes(list);
            return dto;

    }

    @Override
    @Transactional
    public void updateWhitdish(SetmealDto dto) {
        this.updateById(dto);
        //先删除原来的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,dto.getId());
        setmealDishService.remove(lqw);

        //新增
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);

    }
}
